<template>
  <div class="app-container">
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="12">
        <div class="title">神龙湾-项目岗位</div>
      </el-col>
    </el-row> -->
    <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="80px" size="mini">
      <el-form-item label="岗位编号" prop="projectPostNo">
        <el-input v-model="queryParams.projectPostNo" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="岗位名称" prop="projectPostName">
        <el-select v-model="queryParams.projectPostName" placeholder="请选择">
          <el-option v-for="dict in dict.type.cld_project_post" :key="dict.value" :label="dict.label"
                     :value="parseInt(dict.value)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工名称" prop="staffName">
        <el-input v-model="queryParams.staffName" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="员工状态" prop="staffStatus">
        <el-select v-model="queryParams.staffStatus" placeholder="请选择">
          <el-option v-for="item in staffStatusOptions" :key="item.value" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="projectPostList" border>
      <el-table-column align="center" label="岗位编号" prop="projectPostNo" width="200"/>
      <el-table-column align="center" label="岗位名称" prop="projectPostName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目小组" prop="groupName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_groups" :value="scope.row.groupName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="职务" prop="projectDuitsName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="员工姓名" prop="staffName"/>
      <el-table-column align="center" label="职别" prop="jobName"/>
      <el-table-column align="center" label="身份证号码" prop="staffIdcard"/>
      <el-table-column align="center" label="年龄">
        <template slot-scope="scope">
          {{ calculateAge(scope.row.staffIdcard) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="性别" prop="staffSex"/>
      <el-table-column align="center" label="联系方式" prop="staffPhone"/>
      <el-table-column align="center" label="状态" prop="staffStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.staffStatus == 2 && !scope.row.resignTime" type="success">在岗</el-tag>
          <el-tag v-if="scope.row.resignTime" type="danger">待离职</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="保险单号" prop="insuranceOdd"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.staffId" size="mini" type="text"
                     @click="handleAddStaffOrInsurance(scope.row, 1)">添加人员
          </el-button>
          <el-button v-if="!scope.row.insuranceOdd && scope.row.staffId" size="mini" type="text"
                     @click="handleAddStaffOrInsurance(scope.row, 2)">关联保险
          </el-button>
          <el-button v-if="scope.row.staffStatus == 2" size="mini" type="text"
                     @click="handleUnbinding(scope.row)">解绑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>


    <!-- 设置人员 / 关联保险 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" label-width="80px" size="mini">
        <el-form-item v-if="type == 1" :rules="[
          { required: true, message: '请选择人员', trigger: 'blur' },
        ]" label="关联人员" prop="staffName">
          <el-input v-model="form.staffName" :disabled="true" placeholder="请选择">
            <el-button slot="append" icon="el-icon-search" @click="handleStaffOpen">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item v-if="type == 2" :rules="[
          { required: true, message: '请选择保险', trigger: 'blur' },
        ]" label="关联保险" prop="insuranceName">
          <el-input v-model="form.insuranceName" :disabled="true" placeholder="请选择">
            <el-button slot="append" icon="el-icon-search" @click="handleInsuranceOpen">选择</el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择人员 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="staffOpen" append-to-body title="选择人员" width="70%">
      <selected-staff v-if="staffOpen" :params="{ isSetGroupProjectId: projectId }" type="staff"
                      @childInput="handleStaffChange"></selected-staff>
    </el-dialog>

    <!-- 选择保险 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="insuranceOpen" append-to-body title="选择保险" width="70%">
      <selected-insurance v-if="insuranceOpen" type="insurance"
                          @childInput="handleInsuranceChange"></selected-insurance>
    </el-dialog>
  </div>
</template>

<script>
import {getProjectPostList, unbindingPost, updatePost} from '@/api/cld/post'
import {relatedPersonnel} from '@/api/cld/insurance'
import {staffStatusOptions} from '@/assets/staticDict'
import selectedStaff from '../staff/selectedStaff.vue';
import selectedInsurance from '../insurance/selectedInsurance.vue';
import SelectedInsurance1 from '../insurance/selectedInsurance.vue';

export default {
  components: {selectedStaff, selectedInsurance, SelectedInsurance1},
  name: "addWorkPlan",
  dicts: ['cld_project_post', 'cld_project_region', 'cld_project_groups', 'cld_project_duits'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 项目岗位
      projectPostList: [],
      // 员工状态
      staffStatusOptions: staffStatusOptions,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        projectPostNo: null,
        staffStatus: null,
        staffName: null,
        projectPostName: null,
        params: {
          isMasterDuits: 2
        }
      },
      // 总条数
      total: 0,
      open: false,
      title: '',
      // 设置人员
      staffOpen: false,
      // 设置保险
      insuranceOpen: false,
      form: {},
      // 操作类型
      type: 1
    };
  },
  created() {
    const projectId = this.$route.params && this.$route.params.projectId;
    if (projectId) {
      this.queryParams.projectId = projectId;
      this.projectId = projectId;
      this.getList();
    }
  },
  methods: {
    // 查询项目岗位
    getList() {
      this.loading = true;
      getProjectPostList(this.queryParams).then(response => {
        this.loading = false;
        this.projectPostList = response.rows;
        this.total = response.total;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 解绑人员
    handleUnbinding(row) {
      this.$modal.confirm('是否解绑？').then(function () {
        return unbindingPost(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },
    /** 新增按钮操作 */
    handleAddStaffOrInsurance(row, type) {
      this.type = type;
      if (this.type == 1) {
        this.title = '关联人员'
      } else {
        this.title = '关联保险'
      }
      this.form = row;
      this.open = true;
    },
    // 选择人员弹框
    handleStaffOpen() {
      // 获取已添加员工id
      this.notStaffIds = this.projectPostList.map(item => item.staffId)
      this.staffOpen = true;
    },
    // 选择保险弹框
    handleInsuranceOpen() {
      this.insuranceOpen = true;
    },
    // 选择回调
    handleStaffChange(e) {
      this.form.staffId = e.staffId;
      this.form.staffName = e.staffName;
      this.staffOpen = false;
    },
    // 保险选择回调
    handleInsuranceChange(e) {
      this.form.insuranceId = e.insuranceId;
      this.form.insuranceName = e.insuranceName;
      this.form.staffIds = [this.form.staffId];
      this.form.operaStatus = 1;
      this.form.operaTime = new Date();
      this.form.newInsuranceId = e.insuranceId;
      this.insuranceOpen = false;
    },
    // 提交
    submit() {
      if (this.type == 1) {
        updatePost(this.form).then(res => {
          this.getList();
          this.open = false;
          this.$modal.msgSuccess("操作成功");
        })
      } else {
        relatedPersonnel(this.form).then(res => {
          this.getList();
          this.open = false;
          this.$modal.msgSuccess("操作成功");
        })
      }
    }
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}

.title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 20px;
}
</style>
